Vue.component('my-address', {
    props: ['uid', 'page', 'selectedaddress'],
    // Vue 的 Prop 传递会自动转换大小写：
    template: `
  <div class="address-container">
    <!-- 触发按钮 -->
    <el-button 
      type="primary" 
      @click="dialogVisible = true"
      class="address-button"
      v-if=" page != 'me' "
    >
      <i class="el-icon-location-information"></i> 查看地址列表
    </el-button>
<!--    <el-dropdown-item -->
<!--           @click.prevent="isSetAddress"-->
<!--           v-else-->
<!--    >-->
<!--    地址管理</el-dropdown-item>-->

    <!-- 地址信息弹窗 -->
    <el-dialog
      title="我的地址"
      :visible.sync="dialogVisible"
      width="600px"
      class="address-dialog"
    >
      <div class="address-list">
        <div 
          v-for="(address, index) in addressList" 
          :key="index"
          class="address-item"
          ref="addressItems + index"
          :class="{ 'selected': isSelectId === address.id }"
          @click="selectAddress(address)"
        >
          <div class="address-header">
            <span class="address-name">{{ address.name }}</span>
            <span class="address-tag"  v-if="address.isDefaultAddress == 'true'">
              <el-tag size="mini" type="danger">默认</el-tag>
            </span>
          </div>
          <div class="address-content">
            <p class="address-detail">
              <i class="el-icon-location"></i>
              {{ address.province }} {{ address.city }} {{ address.district }} {{ address.detail }}
            </p>
            <p class="address-phone">
              <i class="el-icon-phone"></i>
              {{ address.phone }}
            </p>
          </div>
          <div class="address-actions">
            <el-button size="mini" @click="openEditDialog(index)">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteAddress(index)">删除</el-button>
          </div>
          <el-divider v-if="index < addressList.length - 1"></el-divider>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关 闭</el-button>
        <el-button type="primary" @click="openAddDialog">添加新地址</el-button>
      </span>
    </el-dialog>

    <!-- 编辑地址弹窗 -->
    <el-dialog
      :title="isEditing ? '编辑地址' : '添加新地址'"
      :visible.sync="editDialogVisible"
      width="500px"
    >
      <el-form 
        :model="currentAddress" 
        :rules="addressRules" 
        ref="addressForm"
        label-width="80px"
      >
        <el-form-item label="收货人" prop="name">
          <el-input v-model="currentAddress.name"></el-input>
        </el-form-item>
        
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="currentAddress.phone"></el-input>
        </el-form-item>
        
        <el-form-item label="所在地区" prop="region">
          <el-cascader
            v-model="currentAddress.region"
            :options="regionOptions"
            placeholder="请选择省/市/区"
            style="width: 100%"
          ></el-cascader>
        </el-form-item>
        
        <el-form-item label="详细地址" prop="detail">
          <el-input 
            type="textarea" 
            v-model="currentAddress.detail"
            placeholder="街道门牌、楼层房间号等信息"
          ></el-input>
        </el-form-item>
        
        <el-form-item label="设为默认">
          <el-switch v-model="isDefault"></el-switch>
        </el-form-item>
      </el-form>
      
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button 
          type="primary" 
          @click="submitAddress"
          :loading="submitting"
        >
          确 定
        </el-button>
      </span>
    </el-dialog>
  </div>
    `,
    data() {
        // 手机号验证规则
        const validatePhone = (rule, value, callback) => {
            if (!value) {
                callback(new Error('请输入手机号码'));
            } else if (!/^1[3-9]\d{9}$/.test(value)) {
                callback(new Error('请输入正确的手机号码'));
            } else {
                callback();
            }
            console.log("value")
        };


        return {
            dialogVisible: false,
            editDialogVisible: false,
            isEditing: false,
            submitting: false,
            currentIndex: -1,
            currentAddress: this.getEmptyAddress(),
            addressList: [],
            regionOptions: [
                {
                    value: "北京市",
                    label: "北京市",
                    children: [
                        {
                            value: "北京市",
                            label: "北京市",
                            children: [
                                { value: "东城区", label: "东城区" },
                                { value: "西城区", label: "西城区" },
                                { value: "朝阳区", label: "朝阳区" },
                                { value: "丰台区", label: "丰台区" },
                                { value: "石景山区", label: "石景山区" },
                                { value: "海淀区", label: "海淀区" },
                                { value: "门头沟区", label: "门头沟区" },
                                { value: "房山区", label: "房山区" },
                                { value: "通州区", label: "通州区" },
                                { value: "顺义区", label: "顺义区" },
                                { value: "昌平区", label: "昌平区" },
                                { value: "大兴区", label: "大兴区" },
                                { value: "怀柔区", label: "怀柔区" },
                                { value: "平谷区", label: "平谷区" },
                                { value: "密云区", label: "密云区" },
                                { value: "延庆区", label: "延庆区" }
                            ]
                        }
                    ]
                },
                {
                    value: "天津市",
                    label: "天津市",
                    children: [
                        {
                            value: "天津市",
                            label: "天津市",
                            children: [
                                { value: "和平区", label: "和平区" },
                                { value: "河东区", label: "河东区" },
                                { value: "河西区", label: "河西区" },
                                { value: "南开区", label: "南开区" },
                                { value: "河北区", label: "河北区" },
                                { value: "红桥区", label: "红桥区" },
                                { value: "滨海新区", label: "滨海新区" },
                                { value: "东丽区", label: "东丽区" },
                                { value: "西青区", label: "西青区" },
                                { value: "津南区", label: "津南区" },
                                { value: "北辰区", label: "北辰区" },
                                { value: "武清区", label: "武清区" },
                                { value: "宝坻区", label: "宝坻区" },
                                { value: "宁河区", label: "宁河区" },
                                { value: "静海区", label: "静海区" },
                                { value: "蓟州区", label: "蓟州区" }
                            ]
                        }
                    ]
                },
                {
                    value: "河北省",
                    label: "河北省",
                    children: [
                        {
                            value: "石家庄市",
                            label: "石家庄市",
                            children: [
                                { value: "长安区", label: "长安区" },
                                { value: "桥西区", label: "桥西区" }
                            ]
                        },
                        {
                            value: "唐山市",
                            label: "唐山市",
                            children: [
                                { value: "路南区", label: "路南区" },
                                { value: "路北区", label: "路北区" }
                            ]
                        }
                    ]
                },
                {
                    value: "山西省",
                    label: "山西省",
                    children: [
                        {
                            value: "太原市",
                            label: "太原市",
                            children: [
                                { value: "小店区", label: "小店区" },
                                { value: "迎泽区", label: "迎泽区" }
                            ]
                        },
                        {
                            value: "大同市",
                            label: "大同市",
                            children: [
                                { value: "平城区", label: "平城区" },
                                { value: "云冈区", label: "云冈区" }
                            ]
                        }
                    ]
                },
                {
                    value: "内蒙古自治区",
                    label: "内蒙古自治区",
                    children: [
                        {
                            value: "呼和浩特市",
                            label: "呼和浩特市",
                            children: [
                                { value: "新城区", label: "新城区" },
                                { value: "回民区", label: "回民区" }
                            ]
                        },
                        {
                            value: "包头市",
                            label: "包头市",
                            children: [
                                { value: "东河区", label: "东河区" },
                                { value: "昆都仑区", label: "昆都仑区" }
                            ]
                        }
                    ]
                },
                {
                    value: "辽宁省",
                    label: "辽宁省",
                    children: [
                        {
                            value: "沈阳市",
                            label: "沈阳市",
                            children: [
                                { value: "和平区", label: "和平区" },
                                { value: "沈河区", label: "沈河区" }
                            ]
                        },
                        {
                            value: "大连市",
                            label: "大连市",
                            children: [
                                { value: "中山区", label: "中山区" },
                                { value: "西岗区", label: "西岗区" }
                            ]
                        }
                    ]
                },
                {
                    value: "吉林省",
                    label: "吉林省",
                    children: [
                        {
                            value: "长春市",
                            label: "长春市",
                            children: [
                                { value: "南关区", label: "南关区" },
                                { value: "宽城区", label: "宽城区" }
                            ]
                        },
                        {
                            value: "吉林市",
                            label: "吉林市",
                            children: [
                                { value: "昌邑区", label: "昌邑区" },
                                { value: "龙潭区", label: "龙潭区" }
                            ]
                        }
                    ]
                },
                {
                    value: "黑龙江省",
                    label: "黑龙江省",
                    children: [
                        {
                            value: "哈尔滨市",
                            label: "哈尔滨市",
                            children: [
                                { value: "道里区", label: "道里区" },
                                { value: "南岗区", label: "南岗区" }
                            ]
                        },
                        {
                            value: "齐齐哈尔市",
                            label: "齐齐哈尔市",
                            children: [
                                { value: "龙沙区", label: "龙沙区" },
                                { value: "建华区", label: "建华区" }
                            ]
                        }
                    ]
                },
                {
                    value: "上海市",
                    label: "上海市",
                    children: [
                        {
                            value: "上海市",
                            label: "上海市",
                            children: [
                                { value: "黄浦区", label: "黄浦区" },
                                { value: "徐汇区", label: "徐汇区" },
                                { value: "长宁区", label: "长宁区" },
                                { value: "静安区", label: "静安区" },
                                { value: "普陀区", label: "普陀区" },
                                { value: "虹口区", label: "虹口区" },
                                { value: "杨浦区", label: "杨浦区" },
                                { value: "闵行区", label: "闵行区" },
                                { value: "宝山区", label: "宝山区" },
                                { value: "嘉定区", label: "嘉定区" },
                                { value: "浦东新区", label: "浦东新区" },
                                { value: "金山区", label: "金山区" },
                                { value: "松江区", label: "松江区" },
                                { value: "青浦区", label: "青浦区" },
                                { value: "奉贤区", label: "奉贤区" },
                                { value: "崇明区", label: "崇明区" }
                            ]
                        }
                    ]
                },
                {
                    value: "江苏省",
                    label: "江苏省",
                    children: [
                        {
                            value: "南京市",
                            label: "南京市",
                            children: [
                                { value: "玄武区", label: "玄武区" },
                                { value: "秦淮区", label: "秦淮区" }
                            ]
                        },
                        {
                            value: "无锡市",
                            label: "无锡市",
                            children: [
                                { value: "梁溪区", label: "梁溪区" },
                                { value: "滨湖区", label: "滨湖区" }
                            ]
                        }
                    ]
                },
                {
                    value: "浙江省",
                    label: "浙江省",
                    children: [
                        {
                            value: "杭州市",
                            label: "杭州市",
                            children: [
                                { value: "上城区", label: "上城区" },
                                { value: "拱墅区", label: "拱墅区" }
                            ]
                        },
                        {
                            value: "宁波市",
                            label: "宁波市",
                            children: [
                                { value: "海曙区", label: "海曙区" },
                                { value: "江北区", label: "江北区" }
                            ]
                        }
                    ]
                },
                {
                    value: "安徽省",
                    label: "安徽省",
                    children: [
                        {
                            value: "合肥市",
                            label: "合肥市",
                            children: [
                                { value: "瑶海区", label: "瑶海区" },
                                { value: "庐阳区", label: "庐阳区" }
                            ]
                        },
                        {
                            value: "芜湖市",
                            label: "芜湖市",
                            children: [
                                { value: "镜湖区", label: "镜湖区" },
                                { value: "弋江区", label: "弋江区" }
                            ]
                        }
                    ]
                },
                {
                    value: "福建省",
                    label: "福建省",
                    children: [
                        {
                            value: "福州市",
                            label: "福州市",
                            children: [
                                { value: "鼓楼区", label: "鼓楼区" },
                                { value: "台江区", label: "台江区" }
                            ]
                        },
                        {
                            value: "厦门市",
                            label: "厦门市",
                            children: [
                                { value: "思明区", label: "思明区" },
                                { value: "湖里区", label: "湖里区" }
                            ]
                        }
                    ]
                },
                {
                    value: "江西省",
                    label: "江西省",
                    children: [
                        {
                            value: "南昌市",
                            label: "南昌市",
                            children: [
                                { value: "东湖区", label: "东湖区" },
                                { value: "西湖区", label: "西湖区" }
                            ]
                        },
                        {
                            value: "九江市",
                            label: "九江市",
                            children: [
                                { value: "浔阳区", label: "浔阳区" },
                                { value: "濂溪区", label: "濂溪区" }
                            ]
                        }
                    ]
                },
                {
                    value: "山东省",
                    label: "山东省",
                    children: [
                        {
                            value: "济南市",
                            label: "济南市",
                            children: [
                                { value: "历下区", label: "历下区" },
                                { value: "市中区", label: "市中区" }
                            ]
                        },
                        {
                            value: "青岛市",
                            label: "青岛市",
                            children: [
                                { value: "市南区", label: "市南区" },
                                { value: "市北区", label: "市北区" }
                            ]
                        }
                    ]
                },
                {
                    value: "河南省",
                    label: "河南省",
                    children: [
                        {
                            value: "郑州市",
                            label: "郑州市",
                            children: [
                                { value: "中原区", label: "中原区" },
                                { value: "二七区", label: "二七区" }
                            ]
                        },
                        {
                            value: "洛阳市",
                            label: "洛阳市",
                            children: [
                                { value: "老城区", label: "老城区" },
                                { value: "西工区", label: "西工区" }
                            ]
                        }
                    ]
                },
                {
                    value: "湖北省",
                    label: "湖北省",
                    children: [
                        {
                            value: "武汉市",
                            label: "武汉市",
                            children: [
                                { value: "江岸区", label: "江岸区" },
                                { value: "江汉区", label: "江汉区" }
                            ]
                        },
                        {
                            value: "黄石市",
                            label: "黄石市",
                            children: [
                                { value: "黄石港区", label: "黄石港区" },
                                { value: "西塞山区", label: "西塞山区" }
                            ]
                        }
                    ]
                },
                {
                    value: "湖南省",
                    label: "湖南省",
                    children: [
                        {
                            value: "长沙市",
                            label: "长沙市",
                            children: [
                                { value: "芙蓉区", label: "芙蓉区" },
                                { value: "天心区", label: "天心区" }
                            ]
                        },
                        {
                            value: "株洲市",
                            label: "株洲市",
                            children: [
                                { value: "荷塘区", label: "荷塘区" },
                                { value: "芦淞区", label: "芦淞区" }
                            ]
                        }
                    ]
                },
                {
                    value: "广东省",
                    label: "广东省",
                    children: [
                        {
                            value: "广州市",
                            label: "广州市",
                            children: [
                                { value: "荔湾区", label: "荔湾区" },
                                { value: "越秀区", label: "越秀区" }
                            ]
                        },
                        {
                            value: "深圳市",
                            label: "深圳市",
                            children: [
                                { value: "罗湖区", label: "罗湖区" },
                                { value: "福田区", label: "福田区" }
                            ]
                        }
                    ]
                },
                {
                    value: "广西壮族自治区",
                    label: "广西壮族自治区",
                    children: [
                        {
                            value: "南宁市",
                            label: "南宁市",
                            children: [
                                { value: "青秀区", label: "青秀区" },
                                { value: "兴宁区", label: "兴宁区" }
                            ]
                        },
                        {
                            value: "柳州市",
                            label: "柳州市",
                            children: [
                                { value: "城中区", label: "城中区" },
                                { value: "鱼峰区", label: "鱼峰区" }
                            ]
                        }
                    ]
                },
                {
                    value: "海南省",
                    label: "海南省",
                    children: [
                        {
                            value: "海口市",
                            label: "海口市",
                            children: [
                                { value: "秀英区", label: "秀英区" },
                                { value: "龙华区", label: "龙华区" }
                            ]
                        },
                        {
                            value: "三亚市",
                            label: "三亚市",
                            children: [
                                { value: "海棠区", label: "海棠区" },
                                { value: "吉阳区", label: "吉阳区" }
                            ]
                        }
                    ]
                },
                {
                    value: "重庆市",
                    label: "重庆市",
                    children: [
                        {
                            value: "万州区",
                            label: "万州区",
                            children: [
                                { value: "高笋塘街道", label: "高笋塘街道" },
                                { value: "太白街道", label: "太白街道" }
                            ]
                        },
                        {
                            value: "涪陵区",
                            label: "涪陵区",
                            children: [
                                { value: "敦仁街道", label: "敦仁街道" },
                                { value: "崇义街道", label: "崇义街道" }
                            ]
                        }
                    ]
                },
                {
                    value: "四川省",
                    label: "四川省",
                    children: [
                        {
                            value: "成都市",
                            label: "成都市",
                            children: [
                                { value: "锦江区", label: "锦江区" },
                                { value: "青羊区", label: "青羊区" }
                            ]
                        },
                        {
                            value: "自贡市",
                            label: "自贡市",
                            children: [
                                { value: "自流井区", label: "自流井区" },
                                { value: "贡井区", label: "贡井区" }
                            ]
                        }
                    ]
                },
                {
                    value: "贵州省",
                    label: "贵州省",
                    children: [
                        {
                            value: "贵阳市",
                            label: "贵阳市",
                            children: [
                                { value: "南明区", label: "南明区" },
                                { value: "云岩区", label: "云岩区" }
                            ]
                        },
                        {
                            value: "六盘水市",
                            label: "六盘水市",
                            children: [
                                { value: "钟山区", label: "钟山区" },
                                { value: "六枝特区", label: "六枝特区" }
                            ]
                        }
                    ]
                },
                {
                    value: "云南省",
                    label: "云南省",
                    children: [
                        {
                            value: "昆明市",
                            label: "昆明市",
                            children: [
                                { value: "五华区", label: "五华区" },
                                { value: "盘龙区", label: "盘龙区" }
                            ]
                        },
                        {
                            value: "曲靖市",
                            label: "曲靖市",
                            children: [
                                { value: "麒麟区", label: "麒麟区" },
                                { value: "沾益区", label: "沾益区" }
                            ]
                        }
                    ]
                },
                {
                    value: "西藏自治区",
                    label: "西藏自治区",
                    children: [
                        {
                            value: "拉萨市",
                            label: "拉萨市",
                            children: [
                                { value: "城关区", label: "城关区" },
                                { value: "堆龙德庆区", label: "堆龙德庆区" }
                            ]
                        },
                        {
                            value: "日喀则市",
                            label: "日喀则市",
                            children: [
                                { value: "桑珠孜区", label: "桑珠孜区" },
                                { value: "南木林县", label: "南木林县" }
                            ]
                        }
                    ]
                },
                {
                    value: "陕西省",
                    label: "陕西省",
                    children: [
                        {
                            value: "西安市",
                            label: "西安市",
                            children: [
                                { value: "新城区", label: "新城区" },
                                { value: "碑林区", label: "碑林区" }
                            ]
                        },
                        {
                            value: "宝鸡市",
                            label: "宝鸡市",
                            children: [
                                { value: "渭滨区", label: "渭滨区" },
                                { value: "金台区", label: "金台区" }
                            ]
                        }
                    ]
                },
                {
                    value: "甘肃省",
                    label: "甘肃省",
                    children: [
                        {
                            value: "兰州市",
                            label: "兰州市",
                            children: [
                                { value: "城关区", label: "城关区" },
                                { value: "七里河区", label: "七里河区" }
                            ]
                        },
                        {
                            value: "嘉峪关市",
                            label: "嘉峪关市",
                            children: [
                                { value: "雄关街道", label: "雄关街道" },
                                { value: "钢城街道", label: "钢城街道" }
                            ]
                        }
                    ]
                },
                {
                    value: "青海省",
                    label: "青海省",
                    children: [
                        {
                            value: "西宁市",
                            label: "西宁市",
                            children: [
                                { value: "城中区", label: "城中区" },
                                { value: "城东区", label: "城东区" }
                            ]
                        },
                        {
                            value: "海东市",
                            label: "海东市",
                            children: [
                                { value: "乐都区", label: "乐都区" },
                                { value: "平安区", label: "平安区" }
                            ]
                        }
                    ]
                },
                {
                    value: "宁夏回族自治区",
                    label: "宁夏回族自治区",
                    children: [
                        {
                            value: "银川市",
                            label: "银川市",
                            children: [
                                { value: "兴庆区", label: "兴庆区" },
                                { value: "西夏区", label: "西夏区" }
                            ]
                        },
                        {
                            value: "石嘴山市",
                            label: "石嘴山市",
                            children: [
                                { value: "大武口区", label: "大武口区" },
                                { value: "惠农区", label: "惠农区" }
                            ]
                        }
                    ]
                },
                {
                    value: "新疆维吾尔自治区",
                    label: "新疆维吾尔自治区",
                    children: [
                        {
                            value: "乌鲁木齐市",
                            label: "乌鲁木齐市",
                            children: [
                                { value: "天山区", label: "天山区" },
                                { value: "沙依巴克区", label: "沙依巴克区" }
                            ]
                        },
                        {
                            value: "克拉玛依市",
                            label: "克拉玛依市",
                            children: [
                                { value: "独山子区", label: "独山子区" },
                                { value: "克拉玛依区", label: "克拉玛依区" }
                            ]
                        }
                    ]
                },
                {
                    value: "台湾省",
                    label: "台湾省",
                    children: [
                        {
                            value: "台北市",
                            label: "台北市",
                            children: [
                                { value: "中正区", label: "中正区" },
                                { value: "大同区" }
                            ]
                        },
                        {
                            value: "高雄市",
                            label: "高雄市",
                            children: [
                                { value: "盐埕区", label: "盐埕区" },
                                { value: "鼓山区", label: "鼓山区" }
                            ]
                        }
                    ]
                },
                {
                    value: "香港特别行政区",
                    label: "香港特别行政区",
                    children: [
                        {
                            value: "香港岛",
                            label: "香港岛",
                            children: [
                                { value: "中西区", label: "中西区" },
                                { value: "湾仔区", label: "湾仔区" }
                            ]
                        },
                        {
                            value: "九龙",
                            label: "九龙",
                            children: [
                                { value: "油尖旺区", label: "油尖旺区" },
                                { value: "深水埗区", label: "深水埗区" }
                            ]
                        },
                        {
                            value: "新界",
                            label: "新界",
                            children: [
                                { value: "北区", label: "北区" },
                                { value: "大埔区", label: "大埔区" }
                            ]
                        }
                    ]
                },
                {
                    value: "澳门特别行政区",
                    label: "澳门特别行政区",
                    children: [
                        {
                            value: "澳门半岛",
                            label: "澳门半岛",
                            children: [
                                { value: "花地玛堂区", label: "花地玛堂区" },
                                { value: "圣安多尼堂区", label: "圣安多尼堂区" }
                            ]
                        },
                        {
                            value: "离岛",
                            label: "离岛",
                            children: [
                                { value: "嘉模堂区", label: "嘉模堂区" },
                                { value: "圣方济各堂区", label: "圣方济各堂区" }
                            ]
                        }
                    ]
                }
            ],
            addressRules: {
                name: [
                    { required: true, message: '请输入收货人姓名', trigger: 'blur' },
                    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                ],
                phone: [
                    { required: true, validator: validatePhone, trigger: 'blur' }
                ],
                region: [
                    { required: true, message: '请选择所在地区', trigger: 'change' }
                ],
                detail: [
                    { required: true, message: '请输入详细地址', trigger: 'blur' },
                    { min: 5, max: 100, message: '长度在 5 到 100 个字符', trigger: 'blur' }
                ]
            },
            isSelectId: 0,
            showAddress: false,
            isDefault : false,
        };
    },

    created() {
        // console.log("created")
        this.getAddress();
    },
    methods: {
        getEmptyAddress() {
            return {
                id: "",
                uid: "",
                name: "",
                phone: "",
                region: [],
                province: "",
                city: "",
                district: "",
                detail: "",
                isDefaultAddress: "false"
            };
        },
        isSetAddress() {
            console.log("isSetAddress")
            this.dialogVisible = true;
        },
        getAddress() {
            axios.get('b-user/address/getaddress', {
                params: {
                    uid: this.uid
                }
            }).then(res => {
                if (res.data.code === 1) {
                    const addressList = res.data.data;
                    addressList.forEach(address => {
                        address.region = address.region.split(',');
                    });
                    this.addressList = addressList;
                }
            })
        },
        selectAddress(add){
            const addressCopy = JSON.parse(JSON.stringify(add));
            this.isSelectId = addressCopy.id;
            this.selectedaddress = addressCopy;
            this.selectedaddress.region = this.selectedaddress.region.join(',');
            this.$emit("update:selectedaddress", this.selectedaddress)
            this.showAddress = true;
        },


        openEditDialog(index) {
            this.isEditing = true;
            this.currentIndex = index;
            // 深拷贝当前地址对象，避免直接修改原数据
            this.currentAddress = JSON.parse(JSON.stringify(this.addressList[index]));
            this.editDialogVisible = true;
        },

        openAddDialog() {
            this.isEditing = false;
            this.currentIndex = -1;
            this.currentAddress = this.getEmptyAddress();
            this.editDialogVisible = true;
        },

        submitAddress() {
            this.$refs.addressForm.validate(valid => {
                if (valid) {
                    this.submitting = true;
                    // 处理地区数据
                    const [province, city, district] = this.currentAddress.region;
                    this.currentAddress.province = province;
                    this.currentAddress.city = city;
                    this.currentAddress.district = district;
                    this.currentAddress.region = this.currentAddress.region.join(',');
                    this.currentAddress.isDefaultAddress = this.currentAddress.isDefaultAddress
                    this.currentAddress.uid = this.uid;

                    if (this.isEditing) {
                        // 更新现有地址
                        axios.post('b-user/address/updateAddress', this.currentAddress).then(res => {
                           if (res.data.code === 1) {
                               if (this.currentAddress.isDefaultAddress) {
                                   // 如果设置为默认，先取消其他默认地址
                                   this.addressList.forEach(item => item.isDefaultAddress = false);
                               }
                               // 响应式的更新地址
                               this.$set(this.addressList, this.currentIndex, this.currentAddress);
                               this.$message.success('地址更新成功');
                           }
                        })
                    } else {
                        // currentAddress.isDefault = String(currentAddress.isDefault);
                        if(this.isDefault){
                            this.currentAddress.isDefaultAddress = "true";
                        }else{
                            this.currentAddress.isDefaultAddress = "false";
                        }
                        axios.post('b-user/address/saveAddress', this.currentAddress).then(res => {
                            if (res.data.code === 1) {
                                if (this.currentAddress.isDefault) {
                                    // 如果设置为默认，先取消其他默认地址
                                    this.addressList.forEach(item => item.isDefault = false);
                                }
                                this.addressList.push(this.currentAddress);
                                this.getAddress();
                                this.$message.success('地址添加成功');

                            }
                        })
                    }

                    this.submitting = false;
                    this.editDialogVisible = false;
                } else {
                    return false;
                }
            });
        },

        deleteAddress(index) {
            this.$confirm('确定删除该地址吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                axios.get('b-user/address/deleteAddress', {
                    params: {
                        id: this.addressList[index].id
                    }
                }).then(res => {
                    this.addressList.splice(index, 1);
                    this.$message(res.data.msg);
                })
            }).catch(() => {});
        }
    }

});